/*
 * Copyright 2014 Graham Smith
 */
package uk.co.wobblycogs.vegas.project;

import java.nio.file.Path;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import uk.co.wobblycogs.vegas.AbstractService;

/**
 *
 * @author Graham Smith
 */
public class ProjectService extends AbstractService<Project> {

	public ProjectService() {
	}

	@Override
	public Project find(Long id) {
		EntityManager em = emf.createEntityManager();
		return em.find(Project.class, id);
	}

	public List<Project> findAll() {
		EntityManager em = emf.createEntityManager();
		Query q = em.createNamedQuery(Project.FIND_ALL);
		List<Project> results = q.getResultList();
		return results;
	}

	public Project findByPath(Path path) {
		EntityManager em = emf.createEntityManager();
		TypedQuery<Project> query = em.createNamedQuery(Project.FIND_BY_PATH, Project.class);
		query.setParameter("projectPath", path);
		List<Project> results = query.getResultList();
		Project p = null;
		if (results != null && !results.isEmpty()) {
			p = results.get(0);
		}

		return p;
	}

}
